Skip to content

fix: match params arrays element by element on delegate mocks - #1072

Merged
vbreuss merged 3 commits into
mainfrom
fix/params-delegate-element-matchers
Nov 20, 2026
Merged

vbreuss merged 3 commits into
mainfrom
fix/params-delegate-element-matchers

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Methods with a params parameter get a per-element overload for Setup and Verify, so It.Is(1), It.IsAny<int>() matches the elements one by one. Delegate mocks never emitted it: on a params T[] delegate such a call did not compile, and on a params object[] delegate the element matchers bound to the overload that takes values and were compared as values, so the setup never applied and the verification never matched.

The delegate emitter now adds the same overloads as the method emitter, through the same helpers and with the same priorities: the per-element overload for every arity (also above four parameters), and for a lone params object[] the overload without parameters that keeps Setup().AnyParameters() for a call without arguments. This covers the setup and verify interfaces, their implementations and the verify monitor, in classic and in union mode (a member with a params parameter always has the classic overloads). The whole-array matcher of #1031 still matches the whole array and literal elements still match by value; ParamsObjectArrayTests compares the delegates with the methods for these calls, including params string[].

Without OverloadResolutionPriorityAttribute (classic mode on .NET 8, .NET Framework or netstandard without a polyfill) a call without params arguments, such as Setup(), Setup().AnyParameters() or Setup(It.IsAny<int>()), would be ambiguous between the per-element overload and the one that takes the array as values. Methods already had that problem, so in that case every params T[] member, method or delegate, now also gets an overload that leaves out the params parameter and matches an empty array. With the attribute the generated code is unchanged.

A failed verification with element matchers now lists them, for example Invoke(It.IsAny<int>(), [It.IsAny<object>(), 2]), and the overload without params arguments prints []. The element matcher is built once per verification instead of once per recorded call.

Breaking change, as on methods: on a params T[] delegate, a null array literal next to any matcher or null for another parameter no longer compiles (CS0121 between the whole-array and the per-element overload), for example Setup(It.IsAny<int>(), null), Setup(It.IsAny<int>(), args: null) or Setup(null, null) on (int, params object[]). This also applies to delegates with five or more parameters, with optional parameters, or with an object or int? first parameter. Use It.IsNull<T[]>() for the array instead. Without OverloadResolutionPriorityAttribute, null literals for the elements are ambiguous as well (Setup(null, null) on a lone params string[], Setup(It.IsAny<int>(), null, null)); pass typed values such as (string?)null instead.

public delegate int Logger(int level, params object[] args);

Logger logger = Logger.CreateMock();
logger.Mock.Setup(It.Is(4), It.IsAny<object>(), It.Is<object>(2)).Returns(1);

int result = logger(4, "a", 2); // 1, before: 0
logger.Mock.Verify(It.Is(4), It.IsAny<object>(), It.Is<object>(2)).Once();

@vbreuss vbreuss self-assigned this Nov 19, 2026
@vbreuss vbreuss added the bug Something isn't working label Nov 19, 2026
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Test Results

    47 files  ±  0      47 suites  ±0   1h 51m 18s ⏱️ + 11m 54s
 5 025 tests + 18   5 025 ✅ + 18  0 💤 ±0  0 ❌ ±0 
50 416 runs  +186  50 416 ✅ +186  0 💤 ±0  0 ❌ ±0 

Results for commit 42517fa. ± Comparison against base commit 2ae8c68.

This pull request removes 22 and adds 34 tests. Note that renamed tests count towards both.
 x-myHeader3: baz, True)
)
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
Mockolate.Tests.Web.ItExtensionsTests+IsHttpContentTests+WithHeadersTests ‑ ShouldVerifyStringHeaders(x-myHeader1 : foo
Mockolate.Tests.Web.ItExtensionsTests+IsHttpContentTests+WithHeadersTests ‑ ShouldVerifyStringHeaders(x-myHeader1 : foo
…
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
)
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
)
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
int GetHashCode();, public override bool Equals(object obj), public override int GetHashCode())
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
int GetHashCode();, public override bool Equals(object obj), public override int GetHashCode())
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
bool Equals(object obj);, public override string ToString(), public override bool Equals(object obj))
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
bool Equals(object? obj);
int GetHashCode();, public override string ToString(), public override int GetHashCode(), public override bool Equals(object? obj))
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
int GetHashCode();, public override string ToString(), public override int GetHashCode())
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
bool Equals(object obj);, public override string ToString(), public override bool Equals(object obj))
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
bool Equals(object? obj);
int GetHashCode();, public override string ToString(), public override int GetHashCode(), public override bool Equals(object? obj))
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
int GetHashCode();, public override string ToString(), public override int GetHashCode())
…

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

🚀 Benchmark Results

Compared to main, which ran on the same runner. Times that change by more than 10% are marked.

Memory

Benchmark main PR Δ
Indexer (N=1000) 105,544 B 105,545 B 🔴 +1 B (±0%)

Time

Benchmark main PR Δ
Callback 251.9 ns 224.7 ns 🟢 -11%
CompleteWorkflowBenchmarks
Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1.637 μs 0.0207 μs 0.0183 μs 1.00 5.07 KB 1.00
Imposter 1.911 μs 0.0385 μs 0.0360 μs 1.17 7.21 KB 1.42
TUnitMocks 4.352 μs 0.1398 μs 0.1307 μs 2.66 16.95 KB 3.34
Moq 815.748 μs 3.8723 μs 3.6222 μs 498.44 90.69 KB 17.89
NSubstitute 256.893 μs 1.9169 μs 1.7931 μs 156.97 53.08 KB 10.47
FakeItEasy 288.448 μs 0.7662 μs 0.7167 μs 176.25 63.93 KB 12.61
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 2.70GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

MockCreationBenchmarks
CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 15.94 ns 0.681 ns 0.637 ns 1.00 136 B 1.00
Imposter 88.83 ns 1.135 ns 0.948 ns 5.58 504 B 3.71
TUnitMocks 26.78 ns 0.387 ns 0.302 ns 1.68 216 B 1.59
Moq 1,324.22 ns 16.196 ns 14.357 ns 83.20 2088 B 15.35
NSubstitute 1,864.71 ns 17.331 ns 14.473 ns 117.16 5032 B 37.00
FakeItEasy 1,609.17 ns 15.940 ns 14.910 ns 101.11 2755 B 20.26
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 3.09GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CompleteUnconfiguredBenchmarks
Unconfigured N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 431.0 ns 4.76 ns 3.97 ns 1.00 1.48 KB 1.00
Imposter 1 257.9 ns 1.44 ns 1.34 ns 0.60 1.06 KB 0.72
TUnitMocks 1 492.4 ns 1.60 ns 1.42 ns 1.14 1.81 KB 1.23
Moq 1 114,213.4 ns 880.72 ns 823.83 ns 265.01 9.79 KB 6.63
NSubstitute 1 7,432.0 ns 25.17 ns 21.02 ns 17.24 10.01 KB 6.78
FakeItEasy 1 7,257.2 ns 35.82 ns 33.51 ns 16.84 11.58 KB 7.84
Mockolate 10 1,511.3 ns 5.02 ns 4.45 ns 1.00 3.89 KB 1.00
Imposter 10 946.2 ns 6.56 ns 6.14 ns 0.63 2.89 KB 0.74
TUnitMocks 10 3,163.8 ns 64.99 ns 60.79 ns 2.09 8.47 KB 2.18
Moq 10 128,558.2 ns 471.50 ns 441.04 ns 85.07 24.53 KB 6.30
NSubstitute 10 23,232.9 ns 95.72 ns 89.53 ns 15.37 22.93 KB 5.89
FakeItEasy 10 27,410.9 ns 80.38 ns 67.12 ns 18.14 53.92 KB 13.86
Mockolate 1000 118,570.3 ns 533.36 ns 498.91 ns 1.00 266.86 KB 1.00
Imposter 1000 79,047.7 ns 613.62 ns 573.98 ns 0.67 203.99 KB 0.76
TUnitMocks 1000 289,758.3 ns 569.44 ns 475.50 ns 2.44 691.21 KB 2.59
Moq 1000 1,474,538.0 ns 9,386.62 ns 8,780.25 ns 12.44 1653.01 KB 6.19
NSubstitute 1000 1,733,085.7 ns 3,334.53 ns 2,784.48 ns 14.62 1434.49 KB 5.38
FakeItEasy 1000 2,813,096.2 ns 45,389.32 ns 42,457.20 ns 23.73 4690.81 KB 17.58
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.60GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CompleteMethodBenchmarks
Method N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 77.77 ns 0.827 ns 0.690 ns 1.00 600 B 1.00
Imposter 1 104.27 ns 1.831 ns 1.623 ns 1.34 728 B 1.21
TUnitMocks 1 271.74 ns 4.163 ns 3.476 ns 3.49 2088 B 3.48
Moq 1 56,883.81 ns 813.216 ns 760.683 ns 731.47 14921 B 24.87
NSubstitute 1 3,052.76 ns 56.484 ns 50.071 ns 39.26 9320 B 15.53
FakeItEasy 1 2,993.63 ns 101.583 ns 95.021 ns 38.50 8301 B 13.84
Mockolate 10 241.47 ns 3.349 ns 3.132 ns 1.00 1104 B 1.00
Imposter 10 241.93 ns 5.143 ns 4.811 ns 1.00 1088 B 0.99
TUnitMocks 10 961.99 ns 8.845 ns 7.386 ns 3.98 3840 B 3.48
Moq 10 57,932.40 ns 472.134 ns 441.635 ns 239.95 18905 B 17.12
NSubstitute 10 4,553.12 ns 57.257 ns 50.757 ns 18.86 12344 B 11.18
FakeItEasy 10 4,664.94 ns 79.446 ns 70.426 ns 19.32 16558 B 15.00
Mockolate 1000 16,200.82 ns 204.986 ns 181.715 ns 1.00 56544 B 1.00
Imposter 1000 12,747.47 ns 170.590 ns 159.570 ns 0.79 40688 B 0.72
TUnitMocks 1000 71,627.57 ns 217.036 ns 192.397 ns 4.42 203585 B 3.60
Moq 1000 165,720.48 ns 2,922.293 ns 2,733.515 ns 10.23 431938 B 7.64
NSubstitute 1000 140,777.81 ns 1,641.117 ns 1,535.102 ns 8.69 378037 B 6.69
FakeItEasy 1000 193,607.38 ns 4,597.433 ns 4,075.504 ns 11.95 944541 B 16.70
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.54GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CompletePropertyBenchmarks
Property N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 210.1 ns 1.76 ns 1.65 ns 1.00 904 B 1.00
Imposter 1 144.9 ns 1.57 ns 1.39 ns 0.69 672 B 0.74
TUnitMocks 1 365.5 ns 6.82 ns 5.70 ns 1.74 1672 B 1.85
Moq 1 7,861.9 ns 62.78 ns 58.72 ns 37.43 10569 B 11.69
NSubstitute 1 5,604.2 ns 99.99 ns 93.53 ns 26.68 11688 B 12.93
FakeItEasy 1 5,698.8 ns 89.20 ns 83.44 ns 27.13 11196 B 12.38
Mockolate 10 517.7 ns 5.04 ns 4.71 ns 1.00 1768 B 1.00
Imposter 10 259.3 ns 2.61 ns 2.32 ns 0.50 960 B 0.54
TUnitMocks 10 1,255.5 ns 29.21 ns 27.32 ns 2.43 4024 B 2.28
Moq 10 12,911.1 ns 160.38 ns 150.02 ns 24.94 18505 B 10.47
NSubstitute 10 12,501.2 ns 105.18 ns 93.24 ns 24.15 21553 B 12.19
FakeItEasy 10 13,603.7 ns 115.87 ns 108.38 ns 26.28 30785 B 17.41
Mockolate 1000 33,085.6 ns 190.10 ns 177.82 ns 1.00 96808 B 1.00
Imposter 1000 13,190.5 ns 139.31 ns 130.31 ns 0.40 32641 B 0.34
TUnitMocks 1000 122,488.6 ns 1,539.88 ns 1,440.40 ns 3.70 242489 B 2.50
Moq 1000 540,200.0 ns 7,301.91 ns 6,097.43 ns 16.33 836540 B 8.64
NSubstitute 1000 816,385.4 ns 10,988.49 ns 9,175.89 ns 24.68 1139726 B 11.77
FakeItEasy 1000 1,059,768.6 ns 14,611.02 ns 12,952.29 ns 32.03 2194290 B 22.67
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CompleteIndexerBenchmarks
Indexer N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 524.3 ns 2.43 ns 2.03 ns 1.00 1.61 KB 1.00
Imposter 1 363.5 ns 2.12 ns 1.88 ns 0.69 1.07 KB 0.67
TUnitMocks 1 851.4 ns 2.95 ns 2.76 ns 1.62 2.72 KB 1.69
Moq 1 106,309.9 ns 589.21 ns 522.32 ns 202.76 20.32 KB 12.63
NSubstitute 1 9,859.5 ns 55.32 ns 51.75 ns 18.80 12.78 KB 7.94
FakeItEasy 1 7,993.2 ns 30.61 ns 27.14 ns 15.24 13.27 KB 8.25
Mockolate 10 1,441.5 ns 4.51 ns 4.22 ns 1.00 2.52 KB 1.00
Imposter 10 822.0 ns 2.82 ns 2.35 ns 0.57 2.13 KB 0.84
TUnitMocks 10 2,742.4 ns 11.20 ns 9.35 ns 1.90 5.37 KB 2.13
Moq 10 114,178.6 ns 348.69 ns 326.16 ns 79.21 29.67 KB 11.76
NSubstitute 10 23,921.7 ns 144.30 ns 134.98 ns 16.59 25.58 KB 10.14
FakeItEasy 10 19,150.5 ns 92.13 ns 81.67 ns 13.28 32.47 KB 12.87
Mockolate 1000 106,461.9 ns 280.94 ns 249.05 ns 1.00 103.07 KB 1.00
Imposter 1000 51,788.2 ns 215.42 ns 201.50 ns 0.49 118.14 KB 1.15
TUnitMocks 1000 212,599.0 ns 2,975.05 ns 2,637.30 ns 2.00 293.67 KB 2.85
Moq 1000 821,975.7 ns 3,066.15 ns 2,868.08 ns 7.72 1006.36 KB 9.76
NSubstitute 1000 1,521,829.2 ns 7,963.76 ns 7,059.66 ns 14.29 1465.53 KB 14.22
FakeItEasy 1000 1,236,623.2 ns 5,854.19 ns 5,476.02 ns 11.62 2153 KB 20.89
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
INTEL XEON PLATINUM 8573C 3.00GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CompleteEventBenchmarks
Event Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 125.0 ns 3.59 ns 3.00 ns 1.00 696 B 1.00
Imposter 158.6 ns 2.54 ns 2.25 ns 1.27 512 B 0.74
TUnitMocks 151.2 ns 0.67 ns 0.56 ns 1.21 1392 B 2.00
Moq 9,675.7 ns 66.00 ns 55.11 ns 77.42 12761 B 18.33
NSubstitute 3,850.0 ns 77.16 ns 72.18 ns 30.81 9192 B 13.21
FakeItEasy 105,588.3 ns 883.65 ns 826.57 ns 844.88 15579 B 22.38
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 3.87GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CallbackBenchmarks
Callback Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 224.7 ns 4.58 ns 4.29 ns 1.00 792 B 1.00
Imposter 219.3 ns 1.47 ns 1.38 ns 0.98 616 B 0.78
TUnitMocks 534.3 ns 7.15 ns 6.69 ns 2.38 2056 B 2.60
Moq 106,170.4 ns 486.10 ns 454.70 ns 472.57 9056 B 11.43
NSubstitute 4,806.7 ns 29.23 ns 25.91 ns 21.39 7864 B 9.93
FakeItEasy 4,855.4 ns 29.22 ns 25.91 ns 21.61 6930 B 8.75
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.64GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

AsyncBenchmarks
Async N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 388.9 ns 1.61 ns 1.26 ns 1.00 1016 B 1.00
Imposter 1 501.1 ns 2.10 ns 1.75 ns 1.29 1616 B 1.59
TUnitMocks 1 1,271.1 ns 20.81 ns 18.45 ns 3.27 4464 B 4.39
Moq 1 144,985.5 ns 1,207.71 ns 1,129.69 ns 372.84 23246 B 22.88
NSubstitute 1 13,545.3 ns 58.89 ns 52.21 ns 34.83 19738 B 19.43
FakeItEasy 1 8,779.1 ns 25.12 ns 20.97 ns 22.58 14324 B 14.10
Mockolate 10 1,576.3 ns 7.87 ns 6.57 ns 1.00 3032 B 1.00
Imposter 10 1,558.0 ns 18.40 ns 16.31 ns 0.99 3488 B 1.15
TUnitMocks 10 5,360.5 ns 27.19 ns 22.71 ns 3.40 10536 B 3.47
Moq 10 190,111.7 ns 1,076.01 ns 898.52 ns 120.61 41147 B 13.57
NSubstitute 10 24,517.0 ns 166.12 ns 155.38 ns 15.55 32322 B 10.66
FakeItEasy 10 35,637.2 ns 105.54 ns 88.13 ns 22.61 60493 B 19.95
Mockolate 1000 138,874.1 ns 1,967.04 ns 1,743.73 ns 1.00 224793 B 1.00
Imposter 1000 119,386.3 ns 207.08 ns 172.93 ns 0.86 209409 B 0.93
TUnitMocks 1000 454,913.9 ns 2,757.46 ns 2,444.41 ns 3.28 703994 B 3.13
Moq 1000 3,128,856.7 ns 18,892.03 ns 16,747.29 ns 22.53 1936120 B 8.61
NSubstitute 1000 1,126,344.7 ns 2,846.50 ns 2,523.35 ns 8.11 1341212 B 5.97
FakeItEasy 1000 3,398,972.4 ns 76,044.13 ns 71,131.73 ns 24.48 5051646 B 22.47
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
INTEL XEON PLATINUM 8573C 3.00GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

SequenceBenchmarks
Sequence Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 3.032 μs 0.0207 μs 0.0193 μs 1.00 2.23 KB 1.00
Imposter 2.630 μs 0.0280 μs 0.0262 μs 0.87 2.23 KB 1.00
TUnitMocks 3.414 μs 0.0352 μs 0.0294 μs 1.13 3.27 KB 1.47
Moq 10.926 μs 0.0461 μs 0.0431 μs 3.60 10.79 KB 4.85
NSubstitute 9.725 μs 0.1006 μs 0.0941 μs 3.21 14.29 KB 6.42
FakeItEasy 9.737 μs 0.1281 μs 0.1135 μs 3.21 14.27 KB 6.41
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
INTEL XEON PLATINUM 8573C 3.46GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

Methods with a `params` parameter get a per-element overload for `Setup` and `Verify`, so `It.Is(1), It.IsAny<int>()` matches the elements one by one. Delegate mocks never emitted it: on a `params T[]` delegate such a call did not compile, and on a `params object[]` delegate the element matchers bound to the overload that takes values and were compared as values, so the setup never applied and the verification never matched.

The delegate emitter now adds the same overloads as the method emitter, through the same helpers and with the same priorities: the per-element overload for every arity (also above four parameters), and for a lone `params object[]` the overload without parameters that keeps `Setup().AnyParameters()` for a call without arguments. This covers the setup and verify interfaces, their implementations and the verify monitor. A `params` delegate keeps the classic overloads in union mode as well, so both modes are covered. The whole-array matcher of #1031 still matches the whole array, literal elements still match by value, and the null literals of #1032 keep their meaning; `ParamsObjectArrayTests` now compares the delegates with the methods for all of these calls, including `params string[]`.

As on methods, a `null` literal for the array next to a matcher for another parameter (`Setup(It.IsAny<int>(), null)`) is now ambiguous (CS0121) between the whole-array and the per-element overload; it compiled for delegates only since #1032 and only with `OverloadResolutionPriorityAttribute`. `It.IsNull<object[]>()` expresses it unambiguously.

```csharp
public delegate int Logger(int level, params object[] args);

Logger logger = Logger.CreateMock();
logger.Mock.Setup(It.Is(4), It.IsAny<object>(), It.Is<object>(2)).Returns(1);

int result = logger(4, "a", 2); // 1, before: 0
logger.Mock.Verify(It.Is(4), It.IsAny<object>(), It.Is<object>(2)).Once();
```

Fixes #1045
…utionPriorityAttribute is missing

Without the attribute (classic mode on .NET 8, .NET Framework or netstandard without a polyfill) a call that passes no `params` arguments, such as `Setup()`, `Setup().AnyParameters()` or `Setup(It.IsAny<int>())`, was ambiguous between the per-element overload and the overload that takes the array as values, because both apply only in their expanded form. Methods already had this problem, and the per-element overloads made it a new break for delegates. In that case every `params T[]` member now also gets an overload that leaves out the `params` parameter and matches an empty array; an overload in normal form wins over both expanded forms. With the attribute nothing changes, so the generator now passes it to the interface implementations as well. A sibling that could already own that signature suppresses the overload.

A failed verification with element matchers printed the matcher array's type name; it now lists the matchers like `[It.IsAny<object>(), 2]`, and the overload without `params` arguments prints `[]`. The element matcher is also built once per verification instead of once per recorded call.
@vbreuss
vbreuss force-pushed the fix/params-delegate-element-matchers branch from 540f023 to 42517fa Compare November 20, 2026 05:01
@sonarqubecloud

Copy link
Copy Markdown

@vbreuss
vbreuss merged commit 5d62335 into main Nov 20, 2026
26 checks passed
@vbreuss
vbreuss deleted the fix/params-delegate-element-matchers branch November 20, 2026 06:07
github-actions Bot added a commit that referenced this pull request Nov 20, 2026
github-actions Bot added a commit that referenced this pull request Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Delegates with a params parameter have no per-element matcher overload

1 participant